﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace CandyAdmin.Config.Filters
{
    public class PermissionAccess : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)

        {
            var isDefined = false;
            var controllerActionDescriptor = filterContext.ActionDescriptor as ControllerActionDescriptor;
            if (controllerActionDescriptor != null)
            {
                isDefined = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
                  .Any(a => a.GetType().Equals(typeof(PublicAccess)));
            }
            if (isDefined) return;


            byte[] result;
            filterContext.HttpContext.Session.TryGetValue("user", out result);
            if (result == null)
            {
                filterContext.Result = new RedirectResult("/Admin/User/Login");
                return;
            }
            base.OnActionExecuting(filterContext);


         
        }

    }
   
    
}
